<?php


namespace app\admin\controller;


use app\admin\common\controller\Base;
use app\admin\common\model\Article as artModel;
use think\facade\Request;
use think\facade\Session;

class Article extends Base
{
    public function index(){
        $this->isLogin();
        return $this->redirect('artList');
    }


    //分类列表
    public function artList(){
        //判断是否登录了
        $this->isLogin();
        $admin_id=Session::get('admin_id');
        $admin_level=Session::get('admin_level');
        //查找出说有的栏目
        if($admin_level==1){
            $art=artModel::paginate(5);
        }else{
            $art=artModel::where('id',$admin_id)->paginate(5);
        }
        if(!is_null($art)){
            $this->assign('artList',$art);
        }
        $this->assign('empty','没有任何文章');
        //设置标题
        $this->assign('title','文章列表');
        //渲染
        return $this->fetch();
    }

    //添加分类
    public function artAdd(){
        if(Request::isPost()){
            $data=Request::post();
            artModel::create($data);
            $this->success('添加分类成功','art/artlist');
        }
        //设置标题
        $this->assign('title','添加分类');
        //渲染
        return $this->fetch();
    }


    //分类编辑界面
    public function artEdit(){
        //判断是否登录了
        $this->isLogin();

        $id=Request::param('id');
        $art=artModel::get($id);
        $art['status_num']=$art->getData('status');
        $this->assign('art',$art);
        //设置标题
        $this->assign('title','分类编辑');
        //渲染
        return $this->fetch();
    }

    //处理分类编辑
    public function doEdit(){
        if(Request::isPost()){
            $data=Request::post();
            $id=$data['id'];
            unset($data['id']);
            artModel::where('id',$id)->Update($data);
            $this->success('修改成功','art/index');
        }
    }
}